package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.exception.ApplicationException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author H.
 * @Date 2023/5/22 16:37
 * @Version 1.0
 */
// API 描述信息
@Api(tags = "TestController 测试接口")
// 指定测试根路径映射地址前缀
@RequestMapping("/test")
// 返回JSON格式的Controller
@RestController()
public class TestController {
    // API 方法描述
    @ApiOperation(value = "测试接口1，打印hello")
// 地址映射
    @GetMapping("/hello")
// 具体方法实现
    public String hello() {
        return "Hello, Spring Boot...";
    }
    // 注意：@ApiParam 方法参数的注解，用来描述参数
    @ApiOperation(value = "测试接口3，返回 你好+传入参数")
    @GetMapping("/helloByName")
    public AppResult<String> helloByName(@ApiParam(value = "名字", required =
            true) String name) {
        return AppResult.success("你好：" + name);
    }
    @ApiOperation(value = "测试接口2，跳转到index.html")
    @GetMapping("/index")
    public String index() {
        return "index.html";
    }
    @ApiOperation(value = "测试接口3，返回一个异常结果")
    @GetMapping("/exception")
    public String testException() throws Exception {
        throw new Exception("这是一个Exception");
    }
    @ApiOperation(value = "测试接口3，返回一个自定义异常结果")
    @GetMapping("/appException")
    public String testApplicationException() {
        throw new ApplicationException("这是一个自定义的ApplicationException");
    }
}
